﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace TheCell
{
    public class ClassifyUtils//只初始化一遍，值改变，指针不变
    {
        public static void Classify(List<Cell> cells)
        {
            Lines = Classify(cells, 0);
            Columns = Classify(cells, 1);
            Areas = Classify(cells, 2);
        }
        public static Dictionary<int, List<Cell>> Lines
        {
            get;
            private set;
        }

        public static Dictionary<int, List<Cell>> Columns
        {
            get;
            private set;
        }

        public static Dictionary<int, List<Cell>> Areas
        {
            get;
            private set;
        }

        private static Dictionary<int, List<Cell>> Classify(List<Cell> cells, int type)
        {
            Dictionary<int, List<Cell>> result = new Dictionary<int, List<Cell>>();
            for (int i = 1; i < 10; i++)
            {
                List<Cell> list = null;
                if (result.Keys.Contains(i))//容错，也许会用到，using System.Linq
                {
                    list = result[i];
                }
                else
                {
                    list = new List<Cell>();
                    result.Add(i, list);
                }

                foreach (Cell cell in cells)
                {
                    Cell target = null;
                    switch (type)
                    {
                        case 0:
                            if (cell.Location.X == i)
                            {
                                target = cell;
                            }
                            break;
                        case 1:
                            if (cell.Location.Y == i)
                            {
                                target = cell;
                            }
                            break;
                        case 2:
                            if (cell.Location.Area == i)
                            {
                                target = cell;
                            }
                            break;
                    }

                    if (target != null)//target为空时cells为81位，只抽取9位
                    {
                        list.Add(target);
                    }
                }
            }
            return result;
        }
    }
}